package com.moli.iov.util;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.moli.iov.util.GsonUtil;
import org.springframework.util.StringUtils;

import java.util.*;

/**
 * @ClassName BeanUtils
 * @Description TODO
 * @Author 李伟松
 * @CreateDate 2019-02-26 下午 5:14
 * @UpdateDate 2019-02-26 下午 5:14
 * @Version 1.0
 */
public class JsonUtils {

    private static <T> String toFastjson(T entry){
        if(entry==null){
            return null;
        }
        return JSON.toJSONStringWithDateFormat(entry,"yyyy-MM-dd HH:mm:ss");
    }

    private static <T> String toGson(T entry){
        return GsonUtil.toJsonWithSerializeNulls(entry);
    }

    private static <T> T parseGson(String str, Class<T> clazz) {
        return GsonUtil.parseJsonWithGson(str,clazz);
    }

    private static <T> T parseFastjson(String str, Class<T> clazz) {
        if(StringUtils.isEmpty(str)){
            return null;
        }
        try{
            return JSON.parseObject(str,clazz);
        }catch (Exception e){
            e.printStackTrace();
            return null;
        }
    }

    public static <T> String toJson(T entry){
        return toFastjson(entry);
    }

    public static <T> T parse(String str, Class<T> clazz) {
        return parseFastjson(str,clazz);
    }

    final static Map<Object,Object> map = new HashMap<>();

    public static <T> T parseObject(String str,Class<T> clazz){
        return JSONObject.parseObject(str,new TypeReference<T>() {});
    }
}
